﻿using RepositoryDefinition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryImplementation
{
    public class PostRepository:IPostRepository
    {
        public List<ForumDomainModels.Post> GetPostsByCategory(ForumDomainModels.Category category)
        {
            try
            {
                ForumDAL.PostDAL postContext = new ForumDAL.PostDAL();
                List<ForumDomainModels.Post> posts = postContext.GetPostsByCategory(category);
                return posts;
            }
            catch (ForumDAL.CustomExceptions.NoRowsRetrieved e)
            {
                return null;
            }
        }

        public void InsertPost(ForumDomainModels.Post post)
        {
            ForumDAL.PostDAL postContext = new ForumDAL.PostDAL();
            postContext.InsertPost(post);
        }

        public DateTime GetTimeOfLatestPostByCategory(ForumDomainModels.Category category)
        {
            ForumDAL.PostDAL postContext = new ForumDAL.PostDAL();
            DateTime timeLatestPost = postContext.GetLatestPostTimeToCategory(category);
            return timeLatestPost;
        }

        public int GetPostCountByCategory(ForumDomainModels.Category category)
        {
            ForumDAL.PostDAL postContext = new ForumDAL.PostDAL();
            int postCount = postContext.GetPostCountByCategory(category);
            return postCount;
        }

        public ForumDomainModels.Post GetPostById(ForumDomainModels.Post post)
        {
            try
            {
                ForumDAL.PostDAL postContext = new ForumDAL.PostDAL();
                ForumDomainModels.Post retrievedPost = postContext.GetPostById(post);
                return retrievedPost;
            }
            catch (ForumDAL.CustomExceptions.NoRowsRetrieved e)
            {
                return null;
            }
        }
    }
}
